﻿/// <reference path="typings/jquery/jquery.d.ts"/>
var oData = {
  SelectID: 0
};
//绑定


var pages = pages || {};
(function(p) {
  p.main = {
    init: function() {
      _pageBindings();
    }
  };
  //local functions
  function _pageBindings() {
    $(document).ready(function() {
      $("#FunctionCode").keydown(function(e) {
        if (e.keyCode == 9) {
          e.preventDefault();
          var start = this.selectionStart,
            end = this.selectionEnd;
          var text = this.value;
          var tab = '    ';
          text = text.substr(0, start) + tab + text.substr(start);
          this.value = text;
          this.selectionStart = start + tab.length;
          this.selectionEnd = end + tab.length;
        }
      });
      //绑定提交按钮的处理函数
      $("#submit_form").click(function() {
        addNewFunc();
      });
      //初始化长度
      $("#this_Nav").css("height", $("#this_Cont")[0].height);

      //绑定
      $("#this_Nav").mouseleave(function(e) {
        if (e.pageX < this.offsetLeft || e.pageX > this.offsetLeft + this.offsetWidth || e.pageY > this.offsetTop + this.offsetHeight || e.pageY < this.offsetTop) {
          //离开了区域
          $("#ifdel").hide();
        }
      });
      var getList = {
        nTimes: 0,
        fTryGetList: function() {
          getList.nTimes += 1;
          if (getList.nTimes > 3) {
            $("#items").html("<h1>Unable To Load</h1>");
            return;
          }
          $.ajax({
            url: 'api.asp',
            type: 'POST',
            data: {
              "action": "getList"
            },
            dataType: 'text',
            timeout: 1000,
            async: false,
            error: function() {
              //获取失败，就继续获取
              setTimeout(getList.fTryGetList, 100);
            },
            success: function(result) {

              $("#items").html(result);
            }
          });
        }
      };
      getList.fTryGetList();
      $("#txtSearch").keyup(function() {
        var sTitle = $("#txtSearch").val();
        var sUrlCode;
        if (sTitle.length == 0) {
          sUrlCode = PageLib.AjaxGetText("api.asp", {
            "action": "getList"
          });
        } else {
          sUrlCode = PageLib.AjaxGetText("api.asp", {
            "action": "searchToList",
            "keyword": sTitle
          });
        }

        $("#items").html(sUrlCode);

      });
    });
  }
})(pages);

function refresh() {
  $.ajax({
    url: "lib.asp",
    type: "post",
    data: {
      "action": "delItem",
      "id": oData.SelectID
    },
    success: function(data) {
      var oRet = MyParse(data);
      if (oRet) {
        if (oRet.ref == "ok") {
          //alert(oRet.msg);
        }
      }
    }
  });
  window.location.reload(true);
}

function checkFun() {
  var sFuncName = $("#FunctionName").val();
  var sCode = $("#FunctionCode").val();
  if (!sFuncName) {
    alert("函数必须要有函数名哦！");
    $("#FunctionName").focus();
    return false;
  }
  if (!sCode) {
    alert("既然提交代码，亲，填上代码吧，咱们程序员提交null是几个意思？");
    $("#FunctionCode").focus();
    return false;
  }
  return true;
}

/**
 *
 * @param {String} usrl
 * @param {Object.<String,Number>} option
 */
var PageLib = {
  AjaxGetText: function(url, option) {
    var sResult;
    $.ajax({
      url: url,
      type: 'POST',
      data: option,
      dataType: 'text',
      timeout: 1000,
      async: false,
      error: function() {
        sResult = "";
      },
      success: function(result) {
        sResult = result;
      }
    });
    return sResult;
  }
};